/*******************************************************************************
 * 
 ******************************************************************************/
var addUserWin;
var updateUserWin;
var userAddForm;
var userUpdateForm;
var userGrid;
var u_l_store;
var u_s_store;
var u_cm;
var u_sel_ids = [];
var u_sel_name;

var addRoleWin;
var updateRoleWin;
var roleAddForm;
var roleUpdateForm;
var roleGrid;
var ro_l_store;
var ro_s_store;
var ro_cm;
var ro_sel_ids = [];
var ro_sel_name;

var addResourceWin;
var updateResourceWin;
var searchResourceWin;
var resourceAddForm;
var resourceUpdateForm;
var resourceGrid;
var re_l_store;
var re_s_store;
var re_cm;
var re_sel_ids = [];
var re_sel_name;

var pageSize = 5;

Ext.onReady(function() {

	Ext.QuickTips.init();

	Ext.form.Field.prototype.msgTarget = "side";

	Ext.BLANK_IMAGE_URL = "http://localhost/extjstest/resources/images/default/s.gif";
	/** *************************************************** */

	TopicRecord = Ext.data.Record.create([{
		name : 'id',
		mapping : 'resource.id'
	}, {
		name : 'name',
		mapping : 'resource.type'
	}, {
		name : 'discription',
		mapping : 'resource.value'
	}]);		
	
	resourceAddForm = new Ext.form.FormPanel({
				url : "resourceAction!add.action",
				//url : "resourceAction!update.action",
				method : 'post',
				defaultType : "textfield",
				labelWidth : 55,
				defaults : {
					anchor : "92%"
				},
				baseCls : "x-plain",
				items : [{
							xtype : "hidden",
							name : "resource.id"
						},{
							fieldLabel : "类型",
							value : "URL",
							name : "resource.type",
							allowBlank : false
						}, {
							fieldLabel : "值",
							name : "resource.value",
							allowBlank : false
						}]
			});

	resourceUpdateForm = new Ext.form.FormPanel({
				url : "resourceAction!update.action",
				method : 'post',
				defaultType : "textfield",
				labelWidth : 55,
				defaults : {
					anchor : "92%"
				},
				baseCls : "x-plain",
				items : [{
							xtype : "hidden",
							name : "resource.id"
						}, {
							fieldLabel : "类型",
							name : "resource.type",
							allowBlank : false
						}, {
							fieldLabel : "值",
							name : "resource.value",
							allowBlank : false
						}]

			});

	addResourceWin = new Ext.Window({
				layout : 'fit',
				buttonAlign : 'center',
				width : 400,
				height : 200,
				closeAction : 'hide',
				modal : true,
				plain : true,
				buttons : [{
							text : '保存',
							handler : function() {
								//alert(resourceAddForm.getForm().url);
								resourceAddForm.getForm().submit({
									
											success : function(form, action) {
												resourceAddForm.getForm().reset();
												addResourceWin.hide();
												resourceGrid.getStore().removeAll();
												resourceGrid.getStore().load({
															params : {
																start : 0,
																limit : pageSize
															}
														});
											},
											failure : function(form, action) {
												Ext.Msg.alert('提示', '原因如下：'
																+ "df");
											},

											waitMsg : '保存数据...'
										});
							}
						}, {
							text : '清空',
							handler : function() {
								resourceAddForm.getForm().reset();
							}
						}, {
							text : '关闭',
							handler : function() {
								resourceAddForm.getForm().reset();
								addResourceWin.hide();
							}
						}],
				items : [resourceAddForm]
			});

	updateResourceWin = new Ext.Window({
				layout : 'fit',
				buttonAlign : 'center',
				width : 400,
				height : 200,
				closeAction : 'hide',
				modal : true,
				plain : true,
				buttons : [{
							text : '保存',
							handler : function() {
								resourceUpdateForm.getForm().submit({
											success : function(form, action) {
												resourceUpdateForm.getForm().reset();
												updateResourceWin.hide();
												resourceGrid.getStore().removeAll();
												resourceGrid.getStore().load({
															params : {
																start : 0,
																limit : pageSize
															}
														});
											},
											waitMsg : '保存数据...'
										});
							}
						}, {
							text : '取消',
							handler : function() {
								resourceUpdateForm.getForm().reset();
								updateResourceWin.hide();
							}
						}],
				items : [resourceUpdateForm]
			});

	searchResourceWin = new Ext.Window({
				title : '查找权限',
				layout : 'form',
				width : 300,
				height : 180,
				closeAction : 'hide',
				buttonAlign : 'center',
				labelWidth : 55,
				modal : true,
				plain : true,

				buttons : [{
					text : '查找',
					handler : function() {
						resourceGrid.getStore().removeAll();
						resourceGrid.reconfigure(re_s_store, re_cm);
						//alert("name:" + Ext.getCmp("searchName").getValue());
						re_s_store.load({
									params : {
										start : 0,
										limit : pageSize,
										"resource.id" : Ext.getCmp("searchId")
												.getValue(),
										"resource.value" : Ext.getCmp("searchName")
												.getValue()
									}
								});
						searchResourceWin.hide();
						Ext.getCmp("searchId").setValue("");
						Ext.getCmp("searchName").setValue("");
					}

				}, {
					text : '取消',
					handler : function() {
						searchResourceWin.hide();
						Ext.getCmp("searchId").setValue("");
						Ext.getCmp("searchName").setValue("");
					}
				}],
				items : [{
							fieldLabel : '编号',
							width : 200,
							id : 'searchId',
							name : 'searchId',
							xtype : 'textfield'

						}, {

							fieldLabel : '权限值',
							id : 'searchName',
							name : 'searchName',
							width : 200,
							xtype : 'textfield'

						}]

			});		
			
	re_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			new Ext.grid.CheckboxSelectionModel(), {

				header : "编号",

				align : "center",

				dataIndex : "id"

			}, {

				header : "资源类型",

				align : "center",

				dataIndex : "type"

			}, {

				header : "值",

				width : 500,
				
				dataIndex : "value",

				align : "center"

			}]);

	re_l_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "resourceAction!list.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'resource_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'type',
										mapping : 'type'
									}, {
										name : 'value',
										mapping : 'value'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	re_s_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "resourceAction!search.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'resource_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'type',
										mapping : 'type'
									}, {
										name : 'value',
										mapping : 'value'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	resourceGrid = new Ext.grid.GridPanel({
		renderTo : 'gridPanel',
		store : re_l_store,
		cm : re_cm,
		autoHeight : true,
		title : '资源信息',
		frame : true,
		stripeRows : true,
		tbar : [{
					text : "添加资源",
					handler : function() {
						addResourceWin.show();
					},
					scope : this
				}, "-", {
					text : "修改资源",
					handler : function() {
						var sm = resourceGrid.getSelectionModel();
						if (sm.hasSelection()) {
							updateResourceWin.show();
							try {
								var data = resourceGrid.getSelectionModel()
										.getSelected();
								//alert(data.get("value"));
								var myNewRecord = new TopicRecord({
											'resource.id' : data.get("id"),
											'resource.type' : data.get("type"),
											'resource.value' : data.get("value")
										});
								resourceUpdateForm.getForm().loadRecord(myNewRecord);

							} catch (_err) {
								Ext.Msg.alert("系统提示", _err.description);
								updateResourceWin.hide();
							}
						} else {
							Ext.Msg.alert('信息', "请选中一行！");
						}
					},
					scope : this
				}, "-", {
					text : "删除资源",
					handler : function() {
						var sm = resourceGrid.getSelectionModel();
						if (sm.hasSelection()) {
							Ext.Msg.confirm('信息', '确定要删除?', function(btn) {
										if (btn == 'yes') {
											var _sr = resourceGrid
													.getSelectionModel()
													.getSelections();
											for (var i = 0; i < _sr.length; i++) {
												u_sel_ids[i] = _sr[i].get("id");
											}
											Ext.Ajax.request({
														url : "resourceAction!delete.action",
														params : {
															selectedList : u_sel_ids
														}
													});
											for (var i = 0; i < _sr.length; i++) {
												//alert(_sr[i].get("id"));
												resourceGrid.getStore().remove(resourceGrid.getStore().getAt(_sr[i].get("id")));
											}
											//resourceGrid.getStore().removeAll();
											resourceGrid.getStore().reload({
														params : {
															start : 0,
															limit : pageSize
														}
													});
										} else {
											Ext.Msg.alert('信息', "请选中一行！");
										}
									});
						} else {
							Ext.Msg.alert('信息', "请选中纪录！");
						}
					},
					scope : this
				}],
		enableColumnMove : false,
		selModel : new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : re_l_store
				})
	});

	// resourceGrid.show();
	// addResourceWin.show();
	resourceGrid.getStore().load({
				params : {
					start : 0,
					limit : pageSize
				}
			});

});
